package com.example.rightmost.service.Impl;

import com.example.rightmost.dao.TopicMapper;
import com.example.rightmost.entity.Result;
import com.example.rightmost.entity.Topic;
import com.example.rightmost.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author:javayhch
 * @date:2024/8/25
 * @description:
 */
@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    public TopicMapper topicMapper;

    @Override
    public List<Topic> getTopicList(Topic topic) {
        return topicMapper.getTopicList(topic);
    }

    @Override
    public Result deleteTopic(Integer id) {
        int i = topicMapper.deleteTopic(id);
        if(i > 0){
            return Result.success();
        }else{
            return Result.error("删除失败");
        }
    }

    @Override
    public Result updateStatus(Integer id) {
        int i = topicMapper.updateStatus(id);
        if(i > 0){
            return Result.success();
        }else{
            return Result.error("修改失败");
        }
    }

    @Override
    public Result addTopic(Topic topic) {
        Topic getTopic = topicMapper.selectTopic(topic);;
        if (getTopic == null){
            topic.setCreateTime(LocalDateTime.now());
            int i = topicMapper.addTopic(topic);
            if(i > 0){
                return Result.success();
            }else{
                return Result.error("添加失败");
            }
        } else {
            return Result.error("此话题已存在");
        }
    }

    @Override
    public Result updateTopic(Topic topic) {
        Topic getTopic = topicMapper.selectTopic(topic);
        if (getTopic == null){
            topic.setCreateTime(LocalDateTime.now());
            int i = topicMapper.updateTopic(topic);
            if(i > 0){
                return Result.success();
            }else{
                return Result.error("修改失败");
            }
        } else {
            return Result.error("此分类已存在");
        }
    }

    //gao
    @Override
    public Topic getTopic(Integer id) {
        return topicMapper.getTopic(id);
    }
}
